import router from './router'
import store from './store'

import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar styleort { getToken } from '@/utils/auth' // get token from cookiemport getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login', '/404'] // no redirect whitelist
// 前置守卫判断是否含有token
router.beforeEach((to, from, next) => {
  NProgress.start()
  // 含有token 免除登录直接跳转至首页
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      next()
    }
    // 无token如果在白名单内，可以直接放行，否则跳转至登录页面
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login') // 中转到登录页
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
